package cn.easybuy.web.pre;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.easybuy.entity.News;
import cn.easybuy.service.NewsService;
import cn.easybuy.service.impl.NewsServiceImpl;
import cn.easybuy.util.Page;

public class RightNewsServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		NewsService ns=new NewsServiceImpl();
		List<News> list=null;
		PrintWriter out=response.getWriter();
		String opr=request.getParameter("opr");
		if (opr==null) {
			opr="xx";
		}
		if (!opr.equals("more")) {
			list=ns.getNews();
			String newsJSON=JSON.toJSONStringWithDateFormat(list, "yyyy-MM-dd");
			out.print(newsJSON);
		}
		if (opr.equals("more")) {
			Page p=new Page();
			String currNo=request.getParameter("currNo");
			if (currNo==null||currNo=="") {
				currNo="1";
			}
			int currNoInt=Integer.parseInt(currNo);
			int totalCount=ns.getTotalCount();
			p.setTotalCount(totalCount);
			p.setPageSize(10);
			p.setCurrentPageNumber(currNoInt);
			list=ns.getNewsPageList(p.getCurrentPageNumber(), p.getPageSize());
			p.setNewsList(list);
			String newsPageJSON=JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd");
			out.print(newsPageJSON);
		}
		out.flush();
		out.close();
		
	}

}
